package club.xyes.zkh.ecommerce.web.api.controller;

import club.xyes.zkh.ecommerce.commons.entity.Commodity;
import club.xyes.zkh.ecommerce.commons.entity.CommodityImage;
import club.xyes.zkh.ecommerce.commons.vo.GeneralCollectionResult;
import club.xyes.zkh.ecommerce.commons.vo.GeneralResult;
import club.xyes.zkh.ecommerce.service.CommodityImageService;
import club.xyes.zkh.ecommerce.service.CommodityService;
import club.xyes.zkh.ecommerce.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * Create by 郭文梁 2019/4/17 0017 10:29
 * CommodityImageController
 * 商品图片相关数据访问控制器
 *
 * @author 郭文梁
 * @data 2019/4/17 0017
 */
@RestController
@RequestMapping("/api/commodity-image")
@Api("商品图片相关API接口")
public class CommodityImageController extends BaseController<CommodityImage> {
    private final CommodityImageService service;
    private final CommodityService commodityService;

    public CommodityImageController(CommodityImageService service,
                                    CommodityService commodityService) {
        super(service);
        this.service = service;
        this.commodityService = commodityService;
    }

    /**
     * 上传商品图片
     *
     * @param commodityId 商品ID
     * @param imageFile   商品图片文件
     * @return GR
     * @throws IOException IO异常
     */
    @ApiOperation("上传商品图片")
    @PostMapping("/commodity/{commodityId}/")
    public GeneralResult<CommodityImage> create(@PathVariable("commodityId") Integer commodityId,
                                                MultipartFile imageFile) throws IOException {
        if (imageFile == null || imageFile.isEmpty()) {
            return GeneralResult.badRequest("未上传文件");
        }
        Commodity commodity = commodityService.getRequired(commodityId);
        CommodityImage res = service.create(commodity, imageFile);
        return GeneralResult.ok(res);
    }

    /**
     * 查询某商品的图片
     *
     * @param commodityId 商品ID
     * @return GCR
     */
    @ApiOperation("查询某商品的图片")
    @GetMapping("/commodity/{commodityId}")
    public GeneralCollectionResult<CommodityImage> byCommodityId(@PathVariable("commodityId") Integer commodityId) {
        CommodityImage query = new CommodityImage();
        query.setCommodityId(commodityId);
        List<CommodityImage> images = service.findByQuery(query);
        return GeneralCollectionResult.ok(images);
    }

    /**
     * 删除商品图片
     *
     * @param id 图片ID
     * @return GR
     */
    @ApiOperation("删除商品图片")
    @DeleteMapping("/{id}")
    public GeneralResult<CommodityImage> delete(@PathVariable("id") Integer id) {
        CommodityImage image = service.getRequired(id);
        //暂未做鉴权处理
        //TODO 检查是否有权限
        if (service.deleteById(image.getId())) {
            return GeneralResult.ok(image);
        } else {
            return GeneralResult.error("Unable to delete CommodityImage for id " + id);
        }
    }
}
